<?php


namespace App\Console\Commands\OpenShop;


use App\Enums\OpenShop\InvestmentPromotion\MainProcessEnum;
use App\Enums\OpenShop\MissionTemplateCacheEnum;
use App\Enums\OpenShop\MissionTemplateEnum;
use App\Models\OpenShop\Site\OpenShopMissionTemplateModel;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Cache;

class InitMissionTemplate extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'openShop:init-mission-template';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '开店-初始化任务模板';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $arr = [
            [
                'sort'        => 1,
                'name'        => '招商提交开店订单',
                'missionType' => MissionTemplateEnum::MISSION_TYPE_1,
                'mainStep'    => MainProcessEnum::MAIN_PROCESS_INVESTMENT_SIGN,
                'stepStatus'  => 0,
                'handlerType' => MissionTemplateEnum::HANDLER_TYPE_STAFF,
            ],
            [
                'sort'          => 2,
                'name'          => '商家完成合同支付',
                'missionType'   => MissionTemplateEnum::MISSION_TYPE_1,
                'mainStep'      => MainProcessEnum::MAIN_PROCESS_INVESTMENT_SIGN,
                'stepStatus'    => MissionTemplateEnum::STEP_STATUS_1,
                'handlerType'   => MissionTemplateEnum::HANDLER_TYPE_MERCHANT,
                'handlerDay'    => 7,
                'extendHandler' => MissionTemplateEnum::EXTEND_HANDLER_1,
            ],
            [
                'sort'          => 3,
                'name'          => '商家完成合同签署',
                'missionType'   => MissionTemplateEnum::MISSION_TYPE_1,
                'mainStep'      => MainProcessEnum::MAIN_PROCESS_INVESTMENT_SIGN,
                'stepStatus'    => MissionTemplateEnum::STEP_STATUS_2,
                'handlerType'   => MissionTemplateEnum::HANDLER_TYPE_MERCHANT,
                'handlerDay'    => 7,
                'extendHandler' => MissionTemplateEnum::EXTEND_HANDLER_1,
            ],
            [
                'sort'          => 4,
                'name'          => '指派开发顾问',
                'missionType'   => MissionTemplateEnum::MISSION_TYPE_1,
                'mainStep'      => MainProcessEnum::MAIN_PROCESS_SHOP_LOCATION,
                'stepStatus'    => MissionTemplateEnum::STEP_STATUS_3,
                'handlerType'   => MissionTemplateEnum::HANDLER_TYPE_STAFF,
                'handlerDay'    => 3,
                'extendHandler' => MissionTemplateEnum::EXTEND_HANDLER_0,
            ],
            [
                'sort'          => 5,
                'name'          => '提交选址报告',
                'missionType'   => MissionTemplateEnum::MISSION_TYPE_1,
                'mainStep'      => MainProcessEnum::MAIN_PROCESS_SHOP_LOCATION,
                'stepStatus'    => MissionTemplateEnum::STEP_STATUS_4,
                'handlerType'   => MissionTemplateEnum::HANDLER_TYPE_STAFF,
                'handlerDay'    => 3,
                'extendHandler' => MissionTemplateEnum::EXTEND_HANDLER_1,
            ],
            [
                'sort'          => 6,
                'name'          => '商家确认选址信息',
                'missionType'   => MissionTemplateEnum::MISSION_TYPE_1,
                'mainStep'      => MainProcessEnum::MAIN_PROCESS_SHOP_LOCATION,
                'stepStatus'    => MissionTemplateEnum::STEP_STATUS_5,
                'handlerType'   => MissionTemplateEnum::HANDLER_TYPE_MERCHANT,
                'handlerDay'    => 3,
                'extendHandler' => MissionTemplateEnum::EXTEND_HANDLER_1,
            ],
            [
                'sort'               => 7,
                'name'               => '筹备部确认建店信息',
                'missionType'        => MissionTemplateEnum::MISSION_TYPE_1,
                'mainStep'           => MainProcessEnum::MAIN_PROCESS_SPACE_DESIGN,
                'stepStatus'         => MissionTemplateEnum::STEP_STATUS_6,
                'handlerType'        => MissionTemplateEnum::HANDLER_TYPE_STAFF,
                'handlerDay'         => 3,
                'extendHandler'      => MissionTemplateEnum::EXTEND_HANDLER_0,
                'exceptionHandlerId' => 0,
                'handlerRole'        => MissionTemplateEnum::HANDLER_ROLE_2,
                'handlerRule'        => MissionTemplateEnum::HANDLER_RULE_AREA,
            ],

            [
                'sort'               => 8,
                'name'               => '指派空间设计师',
                'missionType'        => MissionTemplateEnum::MISSION_TYPE_1,
                'mainStep'           => MainProcessEnum::MAIN_PROCESS_SPACE_DESIGN,
                'stepStatus'         => MissionTemplateEnum::STEP_STATUS_7,
                'handlerType'        => MissionTemplateEnum::HANDLER_TYPE_STAFF,
                'handlerDay'         => 3,
                'extendHandler'      => MissionTemplateEnum::EXTEND_HANDLER_0,
                'exceptionHandlerId' => 0,
                'handlerRole'        => MissionTemplateEnum::HANDLER_ROLE_3,
                'handlerRule'        => MissionTemplateEnum::HANDLER_RULE_HQ,
            ],
            [
                'sort'          => 9,
                'name'          => '设计师确认设计模块',
                'missionType'   => MissionTemplateEnum::MISSION_TYPE_1,
                'mainStep'      => MainProcessEnum::MAIN_PROCESS_SPACE_DESIGN,
                'stepStatus'    => MissionTemplateEnum::STEP_STATUS_13,
                'handlerType'   => MissionTemplateEnum::HANDLER_TYPE_STAFF,
                'handlerDay'    => 3,
                'extendHandler' => MissionTemplateEnum::EXTEND_HANDLER_1,
            ],
            [
                'sort'          => 10,
                'name'          => '商家完成设计模块支付',
                'missionType'   => MissionTemplateEnum::MISSION_TYPE_1,
                'mainStep'      => MainProcessEnum::MAIN_PROCESS_SPACE_DESIGN,
                'stepStatus'    => MissionTemplateEnum::STEP_STATUS_14,
                'handlerType'   => MissionTemplateEnum::HANDLER_TYPE_MERCHANT,
                'handlerDay'    => 3,
                'extendHandler' => MissionTemplateEnum::EXTEND_HANDLER_1,
            ],
            [
                'sort'               => 11,
                'name'               => '设计师确认设备模块',
                'missionType'        => MissionTemplateEnum::MISSION_TYPE_1,
                'mainStep'           => MainProcessEnum::MAIN_PROCESS_SPACE_DESIGN,
                'stepStatus'         => MissionTemplateEnum::STEP_STATUS_15,
                'handlerType'        => MissionTemplateEnum::HANDLER_TYPE_STAFF,
                'handlerDay'         => 3,
                'extendHandler'      => MissionTemplateEnum::EXTEND_HANDLER_0,
                'exceptionHandlerId' => 0,
                'handlerRole'        => MissionTemplateEnum::HANDLER_ROLE_3,
                'handlerRule'        => MissionTemplateEnum::HANDLER_RULE_ORDER,
            ],
            [
                'sort'          => 12,
                'name'          => '商家完成设备模块支付',
                'missionType'   => MissionTemplateEnum::MISSION_TYPE_1,
                'mainStep'      => MainProcessEnum::MAIN_PROCESS_SPACE_DESIGN,
                'stepStatus'    => MissionTemplateEnum::STEP_STATUS_16,
                'handlerType'   => MissionTemplateEnum::HANDLER_TYPE_MERCHANT,
                'handlerDay'    => 3,
                'extendHandler' => MissionTemplateEnum::EXTEND_HANDLER_1,
            ],
            [
                'sort'               => 13,
                'name'               => '设计师提交整套图',
                'missionType'        => MissionTemplateEnum::MISSION_TYPE_1,
                'mainStep'           => MainProcessEnum::MAIN_PROCESS_SPACE_DESIGN,
                'stepStatus'         => MissionTemplateEnum::STEP_STATUS_8,
                'handlerType'        => MissionTemplateEnum::HANDLER_TYPE_STAFF,
                'handlerDay'         => 3,
                'extendHandler'      => MissionTemplateEnum::EXTEND_HANDLER_1,
                'exceptionHandlerId' => 0,
                'handlerRole'        => MissionTemplateEnum::HANDLER_ROLE_3,
                'handlerRule'        => MissionTemplateEnum::HANDLER_RULE_ORDER,
            ],
            [
                'sort'          => 14,
                'name'          => '商家支付软装模块',
                'missionType'   => MissionTemplateEnum::MISSION_TYPE_1,
                'mainStep'      => MainProcessEnum::MAIN_PROCESS_SPACE_DESIGN,
                'stepStatus'    => MissionTemplateEnum::STEP_STATUS_17,
                'handlerType'   => MissionTemplateEnum::HANDLER_TYPE_MERCHANT,
                'handlerDay'    => 3,
                'extendHandler' => MissionTemplateEnum::EXTEND_HANDLER_1,
            ],
            [
                'sort'               => 15,
                'name'               => '筹备部确认施工进场',
                'missionType'        => MissionTemplateEnum::MISSION_TYPE_1,
                'mainStep'           => MainProcessEnum::MAIN_PROCESS_ENGINEER_CONSTRUCT,
                'stepStatus'         => MissionTemplateEnum::STEP_STATUS_9,
                'handlerType'        => MissionTemplateEnum::HANDLER_TYPE_STAFF,
                'handlerDay'         => 3,
                'extendHandler'      => MissionTemplateEnum::EXTEND_HANDLER_0,
                'exceptionHandlerId' => 0,
                'handlerRole'        => MissionTemplateEnum::HANDLER_ROLE_2,
                'handlerRule'        => MissionTemplateEnum::HANDLER_RULE_AREA,
            ],
            [
                'sort'               => 16,
                'name'               => '筹备部确认施工完成',
                'missionType'        => MissionTemplateEnum::MISSION_TYPE_1,
                'mainStep'           => MainProcessEnum::MAIN_PROCESS_ENGINEER_CONSTRUCT,
                'stepStatus'         => MissionTemplateEnum::STEP_STATUS_10,
                'handlerType'        => MissionTemplateEnum::HANDLER_TYPE_STAFF,
                'handlerDay'         => 3,
                'extendHandler'      => MissionTemplateEnum::EXTEND_HANDLER_0,
                'exceptionHandlerId' => 0,
                'handlerRole'        => MissionTemplateEnum::HANDLER_ROLE_2,
                'handlerRule'        => MissionTemplateEnum::HANDLER_RULE_AREA,
            ],
            [
                'sort'               => 17,
                'name'               => '指派运营督导',
                'missionType'        => MissionTemplateEnum::MISSION_TYPE_1,
                'mainStep'           => MainProcessEnum::MAIN_PROCESS_SHOP_CHECK,
                'stepStatus'         => MissionTemplateEnum::STEP_STATUS_11,
                'handlerType'        => MissionTemplateEnum::HANDLER_TYPE_STAFF,
                'handlerDay'         => 3,
                'extendHandler'      => MissionTemplateEnum::EXTEND_HANDLER_0,
                'exceptionHandlerId' => 0,
                'handlerRole'        => MissionTemplateEnum::HANDLER_ROLE_2,
                'handlerRule'        => MissionTemplateEnum::HANDLER_RULE_AREA,
            ],
            [
                'sort'               => 18,
                'name'               => '营运部验收确认',
                'missionType'        => MissionTemplateEnum::MISSION_TYPE_1,
                'mainStep'           => MainProcessEnum::MAIN_PROCESS_SHOP_CHECK,
                'stepStatus'         => MissionTemplateEnum::STEP_STATUS_12,
                'handlerType'        => MissionTemplateEnum::HANDLER_TYPE_STAFF,
                'handlerDay'         => 3,
                'extendHandler'      => MissionTemplateEnum::EXTEND_HANDLER_1,
                'exceptionHandlerId' => 0,
                'handlerRole'        => MissionTemplateEnum::HANDLER_ROLE_4,
                'handlerRule'        => MissionTemplateEnum::HANDLER_RULE_AREA,
            ],
            [
                'sort'               => 19,
                'name'               => '开业报告待提交',
                'missionType'        => MissionTemplateEnum::MISSION_TYPE_1,
                'mainStep'           => MainProcessEnum::MAIN_PROCESS_START_BUSINESS,
                'stepStatus'         => MissionTemplateEnum::STEP_STATUS_18,
                'handlerType'        => MissionTemplateEnum::HANDLER_TYPE_STAFF,
                'handlerDay'         => 7,
                'extendHandler'      => MissionTemplateEnum::EXTEND_HANDLER_0,
                'exceptionHandlerId' => 0,
                'handlerRole'        => MissionTemplateEnum::HANDLER_ROLE_4,
                'handlerRule'        => MissionTemplateEnum::HANDLER_RULE_ORDER,
            ],
        ];

        // 需要全量更新的
        $refreshStep = [
            MissionTemplateEnum::STEP_STATUS_8,
        ];

        foreach ($arr as $v) {
            $template = OpenShopMissionTemplateModel::query()->where('stepStatus', $v['stepStatus'])->first();
            if ($template) {

                if (in_array($template->stepStatus, $refreshStep)) {

                    // 全量更新
                    $template->update($v);
                } else {

                    // 更新sort
                    if ($template->sort != $v['sort']) {
                        $template->sort = $v['sort'];
                        $template->save();
                    }
                }

                continue;
            }

            if (!isset($v['sort'])) {
                dd('未配置sort.....', $v);
            }

            // 新增数据
            (new OpenShopMissionTemplateModel($v))->addData();
        }

        Cache::forget(MissionTemplateCacheEnum::getStepStatusSortLists());
    }
}
